package cn.stylefeng.guns.modular.controller;

import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.modular.service.RegisterService;
import cn.stylefeng.guns.modular.utils.MailOperation;
import cn.stylefeng.guns.sys.core.util.SaltUtil;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("")
public class RegisterController extends BaseController {
    @Autowired
    private RegisterService registerService;

    @RequestMapping("/register")
    public String register(){
        return "/register.html";
    }



    @RequestMapping(value="/register" , method = RequestMethod.POST)
    @ResponseBody
    public ResponseData register(Model model, User user, HttpServletRequest request) {

        //获取密码盐
        String salt = SaltUtil.getRandomSalt();
        //md5加密，带盐值
        String addSaltPass = SaltUtil.md5Encrypt(user.getPassword(), salt);
        user.setPassword(addSaltPass);
        user.setSalt(salt);
        user.setCreateTime(new Date());
        //数据库已有存在的姓名
        User user1 = registerService.selectByAccount(user.getAccount());
        if (user1!=null) {
            throw new RequestEmptyException("账号已存在！");
        }

        model.addAttribute("user", user);
        if (ConstantsContext.getAutoOpen()) {
            user.setRoleId("1202563391345344514");
        }
        int rows = registerService.insertRegister(user);
        if (rows == 1) {
            MailOperation operation = new MailOperation();
            String userEmail = "welson_lin@163.com";//你的邮箱地址
            String password = "1314lsw";//你的邮箱授权码
            String host = "smtp.163.com";
            String from = "welson_lin@163.com"; //你的邮箱地址
            String to = user.getEmail();// 收件人

            String subject = "code"; //输入邮件主题
            //邮箱内容
            StringBuffer sb = new StringBuffer();
            System.out.println((int) ((Math.random() * 9 + 1) * 100000));
            System.out.println(to + "===========");
            String yzm = "" + (int) ((Math.random() * 9 + 1) * 100000);
            sb.append("<!DOCTYPE>" + "<div bgcolor='#f1fcfa'   style='border:1px solid #d9f4ee; font-size:14px; line-height:22px; color:#005aa0;padding-left:1px;padding-top:5px;   padding-bottom:5px;'><span style='font-weight:bold;'>温馨提示：</span>"
                    + "<div style='width:950px;font-family:arial;'>欢迎注册日程共享管理系统！您已成为我们中的一员，您的会员号为：<br/><h2 style='color:green'>" + yzm + "</h2><br/>本邮件由系统自动发出，请勿回复。<br/>感谢您的使用。<br/>广东石油化工学院</div>"
                    + "</div>");
            try {
                String res = operation.sendMail(userEmail, password, host, from, to,
                        subject, sb.toString());
                System.out.println(res);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        return SUCCESS_TIP;


    }

    /**
     * 忘记密码
     * @return
     */
    @RequestMapping("/forgetPassword")
    public String forgetPassword(){
        return "/forgetPassword.html";
    }



}
